﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EtlComm;
using System.Data.OleDb;
using System.Data;

namespace ETLHandler
{
    public class SuperIBExtract : IExtracter
    {
        private string _connStr;

        #region IExtracter 成员

        public string ip { set; get; }

        public int TaskID { set; get; }

        public string StationID { set; get; }

        public SuperIBExtract()
        {
            OleDbConnectionStringBuilder cb = new OleDbConnectionStringBuilder();
            cb.Provider = "LCPI.IBProvider";
            cb.Add("Location", @"192.168.7.125:c:\office\\DB\office.gdb");
            cb.Add("User ID", "sysdba");
            cb.Add("Password", "masterkey");
            cb.Add("ctype", "win1251");
            cb.Add("auto_commit", "true");
            _connStr = cb.ToString();
        }

        public System.Data.DataTable GetResult(string strSQL)
        {
            using (OleDbConnection con = new OleDbConnection(ip))
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = con;
                cmd.CommandText = strSQL;
                cmd.Connection.Open();
                if (con.State != ConnectionState.Open)
                    throw new Exception("不能打开Interbase数据库");
                OleDbDataReader myreader = cmd.ExecuteReader();
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                ds.Tables.Add(dt);
                ds.Load(myreader, LoadOption.PreserveChanges, ds.Tables[0]);
                myreader.Close();
                ds.Tables[0].TableName = TaskID.ToString();
                ds.Tables[0].Columns.Add(new DataColumn("StationID", typeof(string)) { DefaultValue = StationID });
                return ds.Tables[0];
            }
        }

        public DateTime GetDBTime()
        {
            using (OleDbConnection con = new OleDbConnection(ip))
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = con;
                cmd.CommandText = "select cast( 'Now' as timestamp) from rdb$database";
                //cmd.CommandText = "select shift_control_id from FUEL_SHIFT_CONTROL where shift_control_id=435";
                cmd.Connection.Open();
                if (con.State != ConnectionState.Open)
                    throw new Exception("不能打开Interbase数据库");
                //object x = cmd.ExecuteScalar();
                DateTime dbtime = (DateTime)cmd.ExecuteScalar();
                return dbtime;
            }
        }

        #endregion

        #region IExtracter 成员


        public int GetMaxID(string table, string pk)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IExtracter 成员


        public string PrimaryKey
        {
            get;
            set;
        }

        #endregion
    }
}
